<?php


namespace core\lib;


use GuzzleHttp\Client;

class BotAPI {

    protected static function httpRequest($path, $post_data) {
        $client = new Client();
        $uri = 'http://' . config('qbot.serve') . "{$path}";
        $r = $client->post($uri, [
            'json' => $post_data,
        ]);
        return json_decode((string)$r->getBody(), true);
    }

    public function sendPrivateMessage(int $user_id, string $message): int {
        return self::httpRequest("/send_private_msg", [
            'user_id' => $user_id,
            'message' => $message,
        ])['data']['message_id'];
    }

    public function sendGroupMessage(int $group_id, string $message): int {
        return self::httpRequest('/send_group_msg', [
                'group_id' => $group_id,
                'message' => $message,
            ])['data']['message_id'] ?? 0;
    }

    public function recallMessage(int $message_id) {
        return self::httpRequest('/delete_msg', [
            'message_id' => $message_id,
        ]);
    }

    public function sendLike(int $user_id, int $times) {
        return self::httpRequest("/send_like", [
            'user_id' => $user_id,
            'times' => $times,
        ]);
    }

    public function getGroupMemberInfo(int $group_id, int $user_id, bool $no_cache = false) {
        return self::httpRequest('/get_group_member_info', [
            'group_id' => $group_id,
            'user_id' => $user_id,
            // 'no_cache' => $no_cache,
        ])['data'];
    }

}
